#include "scale.h"
#include "type.h"
QT_USE_NAMESPACE
Scale::Scale(QObject* parent)
    :QObject(parent),m_timer(new QTimer(this)),serial(new QSerialPort(this)),m_timer2(new QTimer(this)), quit(false)
    ,kg_new(0.0),kg_old(0.0),kg_oldsta(0.0),stable(0),comp(0),ready(0)
{
    qDebug()<<"Open1";
    serial->setPortName(scale_dev);
    serial->setDataBits(QSerialPort::Data8);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
      qDebug()<<"Open2";
    serial->setBaudRate(scale_rate);


    if(!serial->open(QIODevice::ReadWrite))
        return;
    qDebug()<<"Open";
    QObject::connect(m_timer,SIGNAL(timeout()),this,SLOT(time_chack()));
    QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(ReadData()));
   // QObject::connect(m_timer2,SIGNAL(timeout()),this,SLOT(ReadData()));
    m_timer->stop();
    m_timer->setInterval(10);
    m_timer->start();
//    m_timer2->stop();
//    m_timer2->setInterval(1000);
//    m_timer2->start();

   // m_timer->start(100);


}
Scale::~Scale(){
    quit=true;
    serial->close();

}

float Scale::ReadKG(){
    bool ch=true;
//    while (ch){
    QString Text;
    txt+=serial->readLine();
    if(txt.at(txt.size()-1)=='\n'){
        QString Text2=txt.mid(9,7);
        //qDebug()<<txt;
       // qDebug()<<Text2;
        kg=Text2.toFloat(&ch);
        txt.clear();
        qDebug()<<kg;
    }

    return kg;

}

void Scale::clear(){
    serial->flush();
}
void Scale::ReadData(){
   // qDebug()<<"Starting Kg";
    ReadKG();
    //qDebug()<<"Serial Close";

}
void Scale::time_chack(){

        //qDebug()<<"time";
        kg_new=kg;
        if(kg_new != kg_old){
            stable=0;
            count=0;
        }
        if(count>=100){
            stable=1;
            qDebug()<<"stable";
            if(kg_oldsta==0.0 && kg_new != 0.0){
                comp=1;
                ready=0;
                //Show GUI  Complet signal
                qDebug()<<"Complet"<<kg_new;
            }else if(kg_new == 0.0){
                comp=0;
                ready=1;
                //Show Ready signal
                qDebug()<<"Ready";
            }else{
                comp=0;
                ready=0;
            }
            kg_oldsta=kg_new;
            count=0;
        }
        kg_old=kg_new;
        if(stable==0){
            count++;
        }



}


//class KG(threading.Thread):
//    def __init__(self,COM,Rate):
//        self.COM=COM
//        self.Rate=Rate
//        self.serial1 = serial.Serial( self.COM,self.Rate) # open first serial port
//        self.serial1.flushInput()

//        a =Time_check()
//        a.start()
//        print self.serial1.portstr + "KG"
//        threading.Thread.__init__(self)

//    def ReadKG(self):
//        ch=1;

//        while ch:
//            Text = self.serial1.readline()
//            Text= Text[9:-7]
//            global  comp

//            try:
//                global ft
//                ft=float(Text)

//            except ValueError,e:
//                ch=1;
//        return Text
//    def clear(self):

//        self.serial1.flushInput()
//        self.serial1.flushOutput()
//    def run(self):
//        print "Starting kg "
//        global ex_threadflag
//        while(not ex_threadflag):
//            txtKG = self.ReadKG()
//            print txtKG

//        self.serial1.close()
//        print "Serial Kg close"
